<?php

namespace App\Jobs;

use Throwable;
use App\Models\Station;
use Illuminate\Bus\Queueable;
use App\Models\Station\Report;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Statistical\ProjectDimensionalityPatrolReports;
use App\Models\Statistical\StationDimensionalityPatrolReports;

/**
 * 报岗统计逻辑
 *
 * Date: 11/05/2018
 * @author George
 * @package App\Jobs
 */
class StationReportGenerate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

	/**
	 * 定义报岗记录存储属性
	 *
	 * @var Report
	 * Date: 11/05/2018
	 * @author George
	 */
    protected $report;

	/**
	 * StationReportGenerate constructor.
	 * @param $report
	 */
	public function __construct(Report $report)
	{
		$this->report = $report;
	}

	/**
	 * 分析报岗记录，统计生成报表
	 *
	 * Date: 11/05/2018
	 * @author George
	 */
    public function handle()
    {
		try {
			$this->projectDimensionality();
			$this->stationDimensionality();
		} catch (Throwable $exception) {
			Log::error($exception);
		}
	}

	/**
	 * 项目维度报表统计
	 *
	 * Date: 11/05/2018
	 * @author George
	 * @throws \Throwable
	 */
	private function projectDimensionality()
	{
		$record = ProjectDimensionalityPatrolReports::query()->where([
			['company_id', '=', $this->report->company_id],
			['project_id', '=', $this->report->project_id],
			['date', '=', $this->report->created_at->toDateString()]
		])->first();
		if ($record) {
			$record->actual_quantity += 1;
			$record->unreport_count -= 1;
			$record->finish_rate = round($record->actual_quantity / $record->timings_count, 2) * 100;
			$record->saveOrFail();
		} else {
			$timings_count = DB::table('timings')->where('project_id', $this->report->project_id)->count();
			$attributes = [
				'company_id' => $this->report->company_id,
				'project_id' => $this->report->project_id,
				'project_name' => $this->report->project_name,
				'timings_count' => $timings_count,
				'actual_quantity' => 1,
				'unreport_count' => $timings_count - 1,
				'finish_rate' => round(($timings_count - 1) / $timings_count, 2) * 100,
				'date' => $this->report->created_at->toDateString()
			];
			ProjectDimensionalityPatrolReports::create($attributes);
		}
	}

	/**
	 * 岗点维度报表统计
	 *
	 * Date: 11/05/2018
	 * @author George
	 * @throws \Throwable
	 */
	private function stationDimensionality()
	{
		$record = StationDimensionalityPatrolReports::query()->where([
			['company_id', '=', $this->report->company_id],
			['project_id', '=', $this->report->project_id],
			['station_id', '=', $this->report->station_id],
			['date', '=', $this->report->created_at->toDateString()]
		])->first();
		if ($record) {
			$record->actual_quantity += 1;
			$record->unreport_count -= 1;
			$timings_count = $record->actual_quantity + $record->unreport_count;
			$record->finish_rate = round($record->actual_quantity / $timings_count, 2) * 100;
			$record->saveOrFail();
		} else {
			/**
			 * 获取岗点信息
			 * @var Station $station
			 */
			$station = Station::query()->select('name')->findOrFail($this->report->station_id);
			// 获取岗点的时间设置
			$genre = DB::table('genres')->select('genres.name')
				->rightJoin('timings', 'genres.id', '=', 'timings.genre_id')
				->where('timings.id', $this->report->timing_id)->first();
			$timings_count = DB::table('timings')->where('project_id', $this->report->project_id)->count();
			$attributes = [
				'company_id' => $this->report->company_id,
				'project_id' => $this->report->project_id,
				'station_id' => $this->report->station_id,
				'station_name' => $station->name,
				'genre_name' => $genre ? $genre->name : '班次错误',
				'actual_quantity' => 1,
				'unreport_count' => $timings_count - 1,
				'finish_rate' => round(1 / $timings_count, 2) * 100,
				'date' => $this->report->created_at->toDateString(),
			];
			StationDimensionalityPatrolReports::create($attributes);
		}
	}
}
